/*
 * Copyright 2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mx.ymate.modbus;

import com.mx.ymate.modbus.impl.DefaultModbusConfig;
import com.mx.ymate.modbus.impl.ModbusServer;
import net.ymate.platform.core.*;
import net.ymate.platform.core.module.IModule;
import net.ymate.platform.core.module.IModuleConfigurer;
import net.ymate.platform.core.module.impl.DefaultModuleConfigurer;

/**
 * @Author: mengxiang.
 * @Date 2025/11/10.
 * @Time: 16:00.
 * @Description:
 */
public final class Modbus implements IModule, IModbus {

    private static volatile IModbus instance;

    private IApplication owner;

    private IModbusConfig config;

    private boolean initialized;

    private ModbusServer modbusServer;

    public static IModbus get() {
        IModbus inst = instance;
        if (inst == null) {
            synchronized (Modbus.class) {
                inst = instance;
                if (inst == null) {
                    instance = inst = YMP.get().getModuleManager().getModule(Modbus.class);
                }
            }
        }
        return inst;
    }

    public Modbus() {
    }

    public Modbus(IModbusConfig config) {
        this.config = config;
    }

    @Override
    public String getName() {
        return MODULE_NAME;
    }

    @Override
    public void initialize(IApplication owner) throws Exception {
        if (!initialized) {
            this.owner = owner;
            if (config == null) {
                IApplicationConfigureFactory configureFactory = owner.getConfigureFactory();
                if (configureFactory != null) {
                    IApplicationConfigurer configurer = configureFactory.getConfigurer();
                    IModuleConfigurer moduleConfigurer = configurer == null ? null : configurer.getModuleConfigurer(MODULE_NAME);
                    if (moduleConfigurer != null) {
                        config = DefaultModbusConfig.create(moduleConfigurer);
                    } else {
                        config = DefaultModbusConfig.create(DefaultModuleConfigurer.createEmpty(MODULE_NAME));
                    }
                }
            }
            if (!config.isInitialized()) {
                config.initialize(this);
            }
            if (config.isEnabled()) {
                modbusServer = new ModbusServer(config);
                if (config.isAutoStart()) {
                    modbusServer.start();
                }
            }
            initialized = true;
            YMP.showVersion("初始化 mx-ymate-netty-netty-${version} 模块成功", new Version(1, 0, 0, Modbus.class, Version.VersionType.Release));
        }
    }

    @Override
    public boolean isInitialized() {
        return initialized;
    }

    @Override
    public void close() throws Exception {
        if (initialized) {
            initialized = false;
            if (config.isEnabled()) {
//                modbusServer.stop();
            }
            config = null;
            owner = null;
        }
    }

    @Override
    public IApplication getOwner() {
        return owner;
    }

    @Override
    public IModbusConfig getConfig() {
        return config;
    }

    @Override
    public void start() {
       modbusServer.start();
    }


}


